package com.example.myapplication.ui.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.pojo.SingleUser;
import com.example.myapplication.pojo.User;
import com.example.myapplication.R;
import com.example.myapplication.ui.home.HomeActivity;
import com.example.myapplication.ui.home.HomeFragment;
import com.example.myapplication.ui.register.RegisterActivity;
import com.example.myapplication.utils.MySqliteHelper;
import com.example.myapplication.utils.StatusBarCompat;

// 这个是 登录页
public class MainActivity extends AppCompatActivity {
    String username;
    String userpassword;
    Button button;
    TextView forget;
    TextView register;
    TextView name;
    TextView password;
    private String TAG="MainActivity  ";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.login);
        name=findViewById(R.id.name);
        password=findViewById(R.id.password);
        forget=findViewById(R.id.forget);
        register=findViewById(R.id.register);
//        StatusBarCompat.setStatusBarColor(MainActivity.this,0xFFFFFF,50);
        StatusBarCompat.translucentStatusBar(MainActivity.this, true);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                username=name.getText().toString();
                userpassword=password.getText().toString();
                User user=new User();
                MySqliteHelper mySqliteHelper=new MySqliteHelper(getApplicationContext());
                SQLiteDatabase readableDatabase =mySqliteHelper.getReadableDatabase();
//                ContentValues contentValues = new ContentValues();
                //查询 是否有这个用户
                String sql="select * from user where name="+username+" and password ="+userpassword;

//                Log.d(TAG, sql);
                Cursor cursor = readableDatabase.query("user",new String[]{"name","age","password","information"},"name=? and password=?",new String[]{username,userpassword},null,null,null);

               if(cursor.getCount()>0){
                   Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                   while (cursor.moveToNext()){
                       String uname = cursor.getString(cursor.getColumnIndex("name"));
                       String upassword = cursor.getString(cursor.getColumnIndex("password"));
                       Integer age=cursor.getInt(cursor.getColumnIndex("age"));
                       String information=cursor.getString(cursor.getColumnIndex("information"));
                       user.setAge(age);
                       user.setPassword(upassword);
                       user.setInformation(information);
                       user.setName(uname);
                   }
                   Log.d(TAG, user.toString());
                   SingleUser.getInstance().setUser(user);

                   // 关闭数据库
                   cursor.close();
                   readableDatabase.close();

                   Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                   startActivity(intent);
               }else{
                   Toast.makeText(MainActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
               }
                cursor.close();
                readableDatabase.close();
            }
        });


        /**
         * 点击跳转到注册页
         */
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

    }
}